package com.jy.wms.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.dao.WmsPurchaseOrderDetailedMapper;
import com.jy.wms.dao.WmsPurchaseOrderMapper;
import com.jy.wms.pojo.WmsOwnerSupplier;
import com.jy.wms.pojo.WmsPurchaseOrder;
import com.jy.wms.pojo.WmsPurchaseOrderDetailed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: WmsPurchaseOrderDetailedService.java
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author lh
 * @Date Fri Sep 15 17:40:53 GMT+08:00 2017
 */

@Service
public class WmsPurchaseOrderDetailedService extends BaseService<WmsPurchaseOrderDetailedMapper, WmsPurchaseOrderDetailed> {
    @Autowired
    private WmsPurchaseOrderMapper wmsPurchaseOrderMapper;


    /**
     * 插入一条记录
     *
     * @param entity 实体对象
     * @return int
     */
    @BaseServiceDescribe(moduleName = "采购订单", btnName = "增加订单")

    public Integer baseInsert(WmsPurchaseOrderDetailed entity) throws SucessException, ServiceException {


        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date d = new Date();
        String date = df.format(d);// new Date()为获取当前系统时间
        entity.setCjsj(date);
        entity.setId(UUIDUtils.uuid());

        //查询当前采购订单下是否有相同的货品
        Integer a = dao.getMxCountByDdid(entity.getSscgdid(), entity.getHpid(), "");
        if (a > 0) {
            throw new ServiceException("当前采购订单下包含重复货品，修改失败");
        } else {
            //增加明细
            Integer b = dao.baseInsertSelective(entity);
            if (b > 0) {
                throw new SucessException("订单明细增加成功");
            } else {
                throw new ServiceException("订单明细增加失败");
            }
        }
    }





    /**
     * 修改订单明细 （匹配有值的字段）
     *
     * @param entity 主键ID列表
     * @return int
     */
    @BaseServiceDescribe(moduleName = "采购订单", btnName = "修改采购订单明细")
    public Integer baseUpdateMx(WmsPurchaseOrderDetailed entity) throws SucessException, ServiceException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date d = new Date();
        String date = df.format(d);// new Date()为获取当前系统时间
        WmsPurchaseOrder wmsPurchaseOrder = wmsPurchaseOrderMapper.baseSelectByPrimaryKey(entity.getSscgdid());//通过采购订单id查询对象
        if (wmsPurchaseOrder.getShzt().equals("1")) {
            throw new ServiceException("订单已审核，不可以进行编辑");
        } else {
                //查询当前采购订单下是否有相同的货品(如果货品更改了，那么要在数据库查找是否有相同的货品
                // （查询的时候排除当前修改的这条明细，因为当前明细要被更改））)
                Integer a = dao.getMxCountByDdid(entity.getSscgdid(), entity.getHpid(), entity.getId());
                if (a > 0) {
                    throw new ServiceException("当前采购订单下包含重复货品，修改失败");
                } else {
                    //修改明细
                    Integer b = dao.baseUpdateByPrimaryKey(entity);
                    if (b > 0) {
                        throw new SucessException("订单明细修改成功");
                    } else {
                        throw new ServiceException("订单明细修改失败");
                    }
                }
        }
    }


    /**
     * 删除订单明细 （匹配有值的字段）
     *
     * @param idList 明细id集合
     * @param cgddidid 订单航
     * @return int
     */
    @BaseServiceDescribe(moduleName = "采购订单", btnName = "删除采购订单明细")
    public Integer baseDeleteByPrimaryKey(List<String> idList,String cgddidid) throws SucessException, ServiceException {
        WmsPurchaseOrder wmsPurchaseOrder = wmsPurchaseOrderMapper.baseSelectByPrimaryKey(cgddidid);//通过采购订单id查询对象
        //查询当前采购订单下的明细数量，如果只剩下一条明细那么不可以删除
        Integer a=dao.selectCount(new EntityWrapper<WmsPurchaseOrderDetailed>().eq("sscgdid",cgddidid));
        if(a==1){
            throw  new ServiceException("此订单只包含一条明细，不可以删除");
        }else {
            if (wmsPurchaseOrder.getShzt().equals("1")) {
                throw new ServiceException("订单已审核，不可以进行编辑");
            } else {
                Integer count = dao.deleteBatchIds(idList);
                if (count > 0) {
                    throw new SucessException("明细删除成功");
                } else {
                    throw new ServiceException("明细删除失败");
                }
            }
        }
    }












}
